package juhouse.glb.manage.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import juhouse.framework.tools.DoText;
import juhouse.glb.manage.dao.MenuSortDao;
import juhouse.glb.manage.form.MenuSortForm;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class MenuSortAction extends DispatchAction{
	
	public ActionForward menuSortList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			this.menuSortListPage(request);
		return mapping.findForward("menuSortList");
	}
	private void menuSortListPage(HttpServletRequest request)
	{
		MenuSortDao dao = new MenuSortDao();
		String[][] menuSortList = dao.getMenuSortList();
		request.setAttribute("menuSortList",menuSortList);
	}
	
	public ActionForward newMenuSort(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("manageId")!=null)
		{
			saveToken(request);
		}
		return mapping.findForward("menuSortQuery");
	}
	public ActionForward modifyMenuSort(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("manageId")!=null)
		{
			MenuSortDao dao = new MenuSortDao();
			int sortId = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
			String[][] menuSortList = dao.getMenuSortList(sortId);
			request.setAttribute("menuSortList",menuSortList);
			saveToken(request);
		}
		return mapping.findForward("menuSortQuery");
	}
	public ActionForward saveMenuSort(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		MenuSortForm menuSortForm = (MenuSortForm)form;
		if(request.getSession().getAttribute("manageId")!=null)
		{
			MenuSortDao dao = new MenuSortDao();
			int sortId = menuSortForm.getSortId();
			if(sortId==0)
			{
				if (isTokenValid(request, true)) 
				{
					dao.newSaveMenuSort(menuSortForm);
				}
			}else{
				if (isTokenValid(request, true)) 
				{
					dao.modifySaveMenuSort(menuSortForm);
				}
			}
			this.menuSortListPage(request);
		}
		return mapping.findForward("menuSortList");
	}
	public ActionForward deleteMenuSort(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("manageId")!=null)
		{
			int sortId = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
			MenuSortDao dao = new MenuSortDao();
			dao.deleteMenuSort(sortId);
			this.menuSortListPage(request);
		}
		return mapping.findForward("menuSortList");
	}
	public ActionForward moveMenuSort(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int sortId           = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
		int oldDOrder           = NumberUtils.stringToInt(request.getParameter("oldDOrder"), 0);
		int dOrder           = NumberUtils.stringToInt(request.getParameter("dOrder"), -1000);
		if(dOrder!=-1000)
		{
			MenuSortDao dao = new MenuSortDao();
			dao.moveMenuSort(sortId,dOrder,oldDOrder);
		}
		this.menuSortListPage(request);
		return mapping.findForward("menuSortList");
	}
	public ActionForward menuSortValidate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		MenuSortDao dao = new MenuSortDao();
		int sortId   = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
    	String sort = request.getParameter("sort");
    	DoText myDoText = new DoText();
    	sort = myDoText.iso2gb(sort);
        if(dao.menuSortValidate(sortId,sort))
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	request.setAttribute("validateResult", "false");
        }
		return mapping.findForward("validateResult");
	}
}
